package com.freemalll.merchant.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freemalll.merchant.common.CommonEnum;
import com.freemalll.merchant.domain.dto.MerAttachDto;
import com.freemalll.merchant.domain.entity.MerAttach;
import com.freemalll.merchant.domain.entity.MerAttachValue;
import com.freemalll.merchant.domain.vo.MerAttachCheckVO;
import com.freemalll.merchant.domain.vo.MerAttachVO;
import com.freemalll.merchant.service.MerAttachService;
import com.freemalll.merchant.service.MerAttachValueService;
import com.freemalll.merchant.mapper.MerAttachMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 服务层实现。
 *
 * @author songshixiong
 * @since 2025-05-22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MerAttachServiceImpl extends ServiceImpl<MerAttachMapper, MerAttach> implements MerAttachService {

    @Resource
    private MerAttachValueService merAttachValueService;


    @Override
    public List<MerAttachDto> listHistoryByMerId(String merId) {
        return baseMapper.listHistoryByMerId(merId);
    }


    @Override
    public List<MerAttachVO> getLatestAttachByMerId(String merId) {
        return baseMapper.getLatestAttachByMerId(merId);
    }

    @Override
    public void edit(String merId, String imageUrl, CommonEnum.businessPhotoType businessPhotoType, int attachType) {
        MerAttach attach = baseMapper.selectOne(Wrappers.<MerAttach>lambdaQuery()
                .eq(MerAttach::getMerId, merId)
                .eq(MerAttach::getConfigCode, businessPhotoType.name()));
        if (Objects.nonNull(attach)) {
            MerAttachValue one = merAttachValueService.getOne(Wrappers.<MerAttachValue>lambdaQuery().eq(MerAttachValue::getAttachId, attach.getAttachId()));
            if (Objects.nonNull(one)) {
                one.setValValue(imageUrl);
                merAttachValueService.updateById(one);
            } else {
                one = new MerAttachValue();
                one.setAttachId(attach.getAttachId());
                one.setValValue(imageUrl);
                merAttachValueService.save(one);
            }
        } else {
            MerAttach merAttach = new MerAttach();
            merAttach.setAttachType(attachType);
            merAttach.setMerId(merId);
            merAttach.setAttachResult("合格");

            merAttach.setConfigName(businessPhotoType.getDescription());
            merAttach.setConfigCode(businessPhotoType.name());
            baseMapper.insert(merAttach);
            MerAttachValue merAttachValue = new MerAttachValue();
            merAttachValue.setAttachId(merAttach.getAttachId());
            merAttachValue.setValValue(imageUrl);
            merAttachValueService.save(merAttachValue);
        }
    }


    @Override
    public List<MerAttachDto> selectBuJianAttachList(String merId) {
        return baseMapper.selectBuJianAttachList(merId);
    }

    @Override
    public List<MerAttachCheckVO> listCheckListByMerId(String merId) {
        return baseMapper.listCheckListByMerId(merId);
    }


    @Override
    public String selectAttachByMerIdAndConfigCode(String merId, String configCode) {
        return baseMapper.selectAttachByMerIdAndConfigCode(merId, configCode);
    }

    @Override
    public List<String> selectByMerIdAndConfigCode(String merId, String name) {
        return baseMapper.selectByMerIdAndConfigCode(merId, name);
    }

    @Override
    public List<MerAttachDto> queryAttachListByOperate(String merId, int attachType) {
        return baseMapper.queryAttachListByOperate(merId, attachType);
    }

    @Override
    public List<MerAttach> getLatestAttachByMerIdAndAttachType(String merId, List<Integer> list) {
        return baseMapper.getLatestAttachByMerIdAndAttachType(merId, list);
    }

    @Override
    public void batchEdit(String merId, List<String> venueContract, CommonEnum.businessPhotoType businessPhotoType, int attachType) {
        MerAttach merAttach = baseMapper.selectOne(Wrappers.<MerAttach>lambdaQuery()
                .eq(MerAttach::getMerId, merId)
                .eq(MerAttach::getConfigCode, businessPhotoType.name()));
        if (Objects.nonNull(merAttach)) {
            merAttachValueService.remove(Wrappers.<MerAttachValue>lambdaQuery().eq(MerAttachValue::getAttachId, merAttach.getAttachId()));
        } else {
            merAttach = new MerAttach();
            merAttach.setAttachType(attachType);
            merAttach.setMerId(merId);
            merAttach.setAttachResult("合格");
            merAttach.setConfigName(businessPhotoType.getDescription());
            merAttach.setConfigCode(businessPhotoType.name());
            baseMapper.insert(merAttach);
        }
        //新增附件
        for (String imageUrl : venueContract) {
            MerAttachValue merAttachValue = new MerAttachValue();
            merAttachValue.setAttachId(merAttach.getAttachId());
            merAttachValue.setValValue(imageUrl);
            merAttachValueService.save(merAttachValue);
        }
    }

}
